﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Messaging;
using SpreadsheetGear;
using SpreadsheetGear.Commands;
using SpreadsheetGear.Windows.Controls;
using WPFWorkbookDesigner.Events;

namespace WPFWorkbookDesigner.ViewModel
{
    public class PasteSpecialViewModel : WindowViewModelBase
    {
        private PasteOperation _pasteOperation = PasteOperation.None;

        private PasteType _pasteType = PasteType.All;

        private WorkbookView view;
        private bool _transpose;
        private bool _skipBlank;
        private Window _interface;

        public PasteSpecialViewModel() : base()
        {
            WindowClosedCommand = new RelayCommand(WindowClosedHandler);
            PasteCommand = new RelayCommand(Paste);
            OkCommand = new RelayCommand<Window>(Paste);

            Messenger.Default.Register<BroadcastActiveViewEvent>(this, SetActiveView);
        }
        public PasteOperation PasteOperation
        {
            get { return _pasteOperation; }
            set
            {
                _pasteOperation = value;
                RaisePropertyChanged();
            }
        }

        public PasteType PasteType
        {
            get { return _pasteType; }
            set
            {
                _pasteType = value;
                RaisePropertyChanged();
            }
        }


        public bool SkipBlank
        {
            get { return _skipBlank; }
            set
            {
                _skipBlank = value;
                RaisePropertyChanged();
            }
        }

        public bool Transpose
        {
            get { return _transpose; }
            set
            {
                _transpose = value;
                RaisePropertyChanged();
            }
        }

        #region Command

        public RelayCommand<Window> OkCommand { get; }
        public RelayCommand PasteCommand { get; }
        public RelayCommand WindowClosedCommand { get; }
        #endregion
        public override void Cleanup()
        {
            Messenger.Default.Unregister<BroadcastActiveViewEvent>(this, SetActiveView);
            base.Cleanup();
        }

        private void Paste()
        {
            view.Dowork(v =>
            {
                CommandRange command = new CommandRange.PasteSpecial(v.RangeSelection, PasteType, PasteOperation, SkipBlank, Transpose);
                v.ActiveCommandManager.Execute(command);
            });
        }

        private void Paste(Window window)
        {
            Paste();
            Close(window);
        }

        private void SetActiveView(BroadcastActiveViewEvent e)
        {
            view = e.View;
            Interface.Owner = Window.GetWindow(e.View);
        }

        private void WindowClosedHandler()
        {
            Cleanup();
            //MessageBox.Show("Closed Command Event");
        }
    }
}
